import { getUserList, getConsumerList } from '../../api/sysUserRole'
import moment from 'moment'

const state = {
  // 搜索框查询条件
  userSearchDTO: {
    departId: '',
    status: '',
    name: '',
    index: 1,
    pageSize: 10
  },
  // 特殊用户列表
  userList: [],
  // 特殊用户总数
  total: +'',
  // 查看详情的抽屉组件的展示和隐藏
  drawer: false,
  // 抽屉存储的用户详细信息
  userDetail: {}
}

const getters = {
  // 计算属性返回当前条件下用户列表
  get_userList (state) {
    return state.userList
  }
}

const mutations = {
  // 设置用户列表
  set_userList (state, list) {
    list.forEach(item => {
      item.createTime = moment(item.createTime).format('YYYY-MM-DD HH:mm')
    })
    state.userList = list
  },
  // 设置当前分页
  set_userSearchDTO_index (state, index) {
    state.userSearchDTO.index = index
  },
  // 设置当前页大小
  set_userSearchDTO_pageSize (state, pageSize) {
    state.userSearchDTO.pageSize = pageSize
  },
  // 设置用户总数
  set_total (state, total) {
    state.total = total
  },
  // 设置抽屉的展示隐藏
  set_drawer (state, boolean) {
    state.drawer = boolean
  },
  // 设置抽屉存储的用户信息
  set_userDetail (state, obj) {
    obj.createTime = moment(obj.createTime).format('YYYY-MM-DD')
    state.userDetail = obj
  }
}

const actions = {
  // 请求获得用户列表信息
  getUserList ({ state, commit }) {
    return new Promise((resolve, reject) => {
      getUserList(state.userSearchDTO).then(res => {
        if (res.data.code === 200 && res.data.data) {
          commit('set_userList', res.data.data.data)
          commit('set_total', res.data.data.count)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 请求获得组织列表
  getConsumerList (context, departSearchDTO) {
    return new Promise((resolve, reject) => {
      getConsumerList(departSearchDTO).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
